from itertools import chain

from django.shortcuts import render
from rest_framework import authentication
from rest_framework.views import APIView

from web.models import RecentTipLog, ProjectUser, Project


def index(request):
    if request.method == 'GET':
        result = None
        if request.web.user:
            pids = ProjectUser.objects.filter(user=request.web.user).values_list("project_id",flat=True)
            create_pids = Project.objects.filter(creator=request.web.user).values_list("id",flat=True)
            pids = list(pids)
            create_pids = list(create_pids)
            pids = list(chain(pids, create_pids))
            result = [(result.project.name, result.log_content, result.log_url, result.create_datetime)
                      for result in RecentTipLog.objects.filter(project_id__in=pids).order_by('-create_datetime')]
        return render(request, 'web/index.html', {'result': result})

# class index(APIView):
#     authentication_classes = [authentication.TokenAuthentication]
#     def get(self, request, format=None):
#         """
#         Return a list of all users.
#
#         """
#         result = [(result.project.name,result.log_content,result.log_url,result.create_datetime)
#                      for result in RecentTipLog.objects.all().order_by('-create_datetime')]
#         print('result:',result)
#         return render(request, 'web/index.html', {'result': result})